package main

import (
	"fmt"
	"github.com/go-xorm/xorm"
	_ "github.com/lib/pq"
	"log"
	"strings"
	"github.com/snluu/uuid"
)

const (
	host     = "47.94.167.248"
	port     = 5432
	user     = "postgres"
	password = "postgres"
	dbName   = "goxx"
)

func getDBEngine() *xorm.Engine {
	psqlInfo := fmt.Sprintf("host=%s port=%d user=%s password=%s dbname=%s sslmode=disable", host, port, user, password, dbName)
	//格式
	engine, err := xorm.NewEngine("postgres", psqlInfo)
	if err != nil {
		log.Fatal(err)
		return nil
	}
	engine.ShowSQL(true) //菜鸟必备

	err = engine.Ping()
	if err != nil {
		log.Fatal(err)
		return nil
	}
	fmt.Println("connect postgresql success")
	return engine
}

func UUID1() string {
	return strings.Replace(uuid.Rand().Hex(), "-", "", -1)
}

type Xx struct {
	Id   string
	Name string
	Age  int
}

//查询所有
func selectAll() {
	var x []Xx
	engine := getDBEngine()
	engine.SQL("select * from xx").Find(&x)
	fmt.Println(x)
	fmt.Println(x[0].Name)
}

//条件查询
func selectX(age int) {
	var x []Xx
	engine := getDBEngine()
	engine.SetSchema("xxs")
	engine.Where("xx.age=?", age).Find(&x)
	fmt.Println(x)
}

//可以用Get查询单个元素
func selectOne(id string) {
	var x Xx
	engine := getDBEngine()
	engine.Alias("u").Where("u.id=?", id).Get(&x)
	//engine.Id(id).Get(&x)
	fmt.Println(x)
}

//添加
func InsertXx(x *Xx) bool {
	engine := getDBEngine()
	rows, err := engine.Insert(x)
	if err != nil {
		log.Println(err)
		return false
	}
	if rows == 0 {
		return false
	}
	return true
}

//删除(根据名称删除)
func DeleteXx(name string)  bool {
	x := Xx{
		Name: name,
	}
	engine := getDBEngine()
	rows,err := engine.Delete(&x)
	if err != nil {
		log.Println(err)
		return false
	}
	if rows == 0 {
		return false
	}
	return true
}

//利用sql删除
func UpXxBySQL(name string) bool {
	engine := getDBEngine()
	result,err := engine.Exec("update xx set age=1123  where name=?",name)
	if err != nil {
		log.Println(err)
		return false
	}
	rows,err :=result.RowsAffected()
	if err == nil && rows >0 {
		return true
	}
	return false
}

//利用sql删除
func DeleteXxBySQL(name string) bool {
	engine := getDBEngine()
	result,err := engine.Exec("delete from xx where name=?",name)
	if err != nil {
		log.Println(err)
		return false
	}
	rows,err :=result.RowsAffected()
	if err == nil && rows >0 {
		return true
	}
	return false
}

//更新
func UpdateXx(x *Xx) bool {
	engine := getDBEngine()
	//Update(bean interface{}, condiBeans ...interface{}) bean是需要更新的bean,condiBeans是条件
	rows,err := engine.Update(x,Xx{Id:x.Id})
	if err != nil {
		log.Println(err)
		return false
	}
	if rows > 0 {
		return true
	}
	return false
}

//利用session进行增删改
//用session的好处就是可以事务处理
func SessionXx(x *Xx) {
	engine := getDBEngine()
	session := engine.NewSession()
	session.Begin()
	_,err := session.Insert(x)
	if err != nil {
		session.Rollback()
		log.Fatal(err)
	}
	//
	//x.Name="windows"
	//_,err = session.Update(x,Xx{Id:x.Id})
	//if err != nil {
	//	session.Rollback()
	//	log.Fatal(err)
	//}
	//
	//_,err = session.Delete(x)
	//if err != nil {
	//	session.Rollback()
	//	log.Fatal(err)
	//}

	err = session.Commit()
	if err != nil {
		log.Fatal(err)
	}

}

func main() {
	//selectAll()
	//selectX(1)
	//selectOne("f3cb6b572d164608945ade8201eac012")

	//
	//x := &Xx{
	//	Id:   UUID1(),
	//	Name: "lalaal12",
	//	Age:  12,
	//}
	//InsertXx(x)

	//DeleteXx("lalaal1")

	//UpXxBySQL("bbbbb")
	//DeleteXxBySQL("lalaal12")

	//x := &Xx{
	//	Id:   "40e971aef4bc4fa2973ef9240100f2ff",
	//	Name: "aaaaa",
	//	Age:  111,
	//}

	//UpdateXx(x)

	//x := &Xx{
	//	Id:   UUID1(),
	//	Name: "aaaaa",
	//	Age:  111,
	//}
	//SessionXx(x)

	//
	//var x []Xx
	//engine := getDBEngine()
	//engine.Find(&x)
	//fmt.Println(x)
	//fmt.Println(x[0].Age)

	//selectX(333)
}
